package services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.NguoiDungPOJO;
import pojo.PairValue;
import pojo.TinNhanPOJO;
import util.ClientSender;
import util.Common;
import DataAccess.NguoiDungDA;
import DataAccess.TinNhanDA;

/**
 * Servlet implementation class OperationsServlet
 */
@WebServlet("/OperationsServlet")
public class OperationsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OperationsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		PrintWriter out = response.getWriter();
		String cmd = getParameter(request, Protocol.CMD);
		System.out.println("CMD=" + cmd);
		try {
			if (cmd.equals(Protocol.KICK_MEMBER_OUT_MESSAGE)) {
				int time = Integer.parseInt(getParameter(request, Protocol.TIME_VOTE_KICK));
				String userNameBeKick = getParameter(request, Protocol.USERNAME_BE_KICK);
				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.TIME_VOTE_KICK, time + ""));
				params.add(new PairValue(Protocol.USERNAME_BE_KICK, userNameBeKick));

				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);

				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
				ClientSender.send(dsIDDevices, params, 5);
				System.out.println("KICK_MEMBER_OUT_MESSAGE: bn.gui=" + nguoiGui + " time=" + time + " username="
						+ userNameBeKick);
			} else if (cmd.equals(Protocol.KICK_MEMBER_OUT_FEEDBACK)) {
				String vote = getParameter(request, Protocol.VOTE_FEEDBACK_KICK);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);

				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.VOTE_FEEDBACK_KICK, vote));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));

				ClientSender.send(regID, params, 5);
				System.out.println("KICK_MEMBER_OUT_FEEDBACK: bn.gui=" + nguoiGui + "bn.nhan=" + nguoiNhan + " vote=" + vote);
			} else if (cmd.equals(Protocol.KICK_MEMBER_OUT_RESULT)) {
				String voteResult = getParameter(request, Protocol.VOTE_KICK_RESULT);
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String userNameKick = getParameter(request, Protocol.USERNAME_BE_KICK);
				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.VOTE_KICK_RESULT, voteResult));
				params.add(new PairValue(Protocol.USERNAME_BE_KICK, userNameKick));
				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);

				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
				ClientSender.send(dsIDDevices, params, 5);
				System.out.println("KICK_MEMBER_OUT_RESULT: bn.gui=" + nguoiGui + " vote=" + voteResult);
			} else if (cmd.equals(Protocol.CHAT_PEER_MESSAGE)) {
				String content = getParameter(request, Protocol.CONTENT_PEER_MESS);
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);

				String[] sTemp = content.split("/");

				String noiDung = sTemp[1];
				TinNhanPOJO tinNhan = new TinNhanPOJO();

				tinNhan.setNguoiGui(nguoiGui);
				tinNhan.setNguoiNhan(nguoiNhan);
				tinNhan.setNoiDung(noiDung);
				tinNhan.setLoai(0);

				TinNhanDA tinNhanDA = new TinNhanDA();
				tinNhanDA.insert(tinNhan);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));
				params.add(new PairValue(Protocol.CONTENT_PEER_MESS, content));
				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);
				ClientSender.send(regID, params, 15);
				System.out.println("CHAT_PEER_MESSAGE: bn.gui=" + nguoiGui + "nguoinhan=" + nguoiNhan + " content=" + noiDung);
			} else if (cmd.equals(Protocol.STOP_TALKING_ONE_MEMBER)) {
				String time = getParameter(request, Protocol.TIME_STOP_TALK);
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));
				params.add(new PairValue(Protocol.TIME_STOP_TALK, time));
				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);
				ClientSender.send(regID, params, 5);
				System.out.println("STOP_TALKING_ONE_MEMBER: bn.gui=" + nguoiGui + "Ngnhan " + nguoiNhan + " time=" + time);
			} else if (cmd.equals(Protocol.ACCEPT_TALKING_ONE_MEMBER)) {

				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));

				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);
				ClientSender.send(regID, params, 10);
				System.out.println("ACCEPT_TALKING_ONE_MEMBER: bn.gui=" + nguoiGui + "Ngnhan " + nguoiNhan);
			} else if (cmd.equals(Protocol.CHAT_FEEDBACK_NON_RULE)) {
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));

				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);
				ClientSender.send(regID, params, 10);
				System.out.println("CHAT_FEEDBACK_NON_RULE: bn.gui=" + nguoiGui + "Ngnhan " + nguoiNhan);
			}//===========================
			else if (cmd.equals(Protocol.GROUP_BIEU_QUYET)) {// bieu quyet
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));
				String time = getParameter(request, Protocol.TIME_TO_VOTE);
				String content = getParameter(request, Protocol.CONTENT_TO_VOTE);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.TIME_TO_VOTE, time));
				params.add(new PairValue(Protocol.CONTENT_TO_VOTE, content));

				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);

				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
				ClientSender.send(dsIDDevices, params, 5);
				System.out.println("GROUP_BIEU_QUYET: bn.gui=" + nguoiGui);
			}
			else if (cmd.equals(Protocol.BIEU_QUYET_FEEDBACK)) {// bieu quyet feedback
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);
				String result = getParameter(request, Protocol.RESULT_TO_VOTE);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));
				params.add(new PairValue(Protocol.RESULT_TO_VOTE, result));

				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);
				ClientSender.send(regID, params, 7);
				System.out.println("BIEU_QUYET_FEEDBACK: bn.gui=" + nguoiGui);
			}
			else if (cmd.equals(Protocol.RESULT_BIEU_QUYET)) {// KQ bieu quyet
				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String result = getParameter(request, Protocol.FINAL_RESULT_VOTE);
				String content = getParameter(request, Protocol.CONTENT_TO_VOTE);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.FINAL_RESULT_VOTE, result));
				params.add(new PairValue(Protocol.CONTENT_TO_VOTE, content));
				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);
				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
				ClientSender.send(dsIDDevices, params, 10);
				System.out.println("RESULT_BIEU_QUYET: bn.gui=" + nguoiGui+" result="+result);
			}
			else if (cmd.equals(Protocol.GROUP_DANH_GIA)) {// danh gia

				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));
				ArrayList<PairValue> params = new ArrayList<PairValue>();
				
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);

				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
				ClientSender.send(dsIDDevices, params, 10);
				System.out.println("GROUP_DANH_GIA: bn.gui=" + nguoiGui );
			}
			else if (cmd.equals(Protocol.DANH_GIA_FEEDBACK)) {// KQ bieu quyet
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);
				String result = getParameter(request, Protocol.RESULT_TO_ESTIMATE);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.NGUOI_NHAN, nguoiNhan));
				params.add(new PairValue(Protocol.RESULT_TO_ESTIMATE, result));

				String regID = new NguoiDungDA().getDeviceID(nguoiNhan);
				ClientSender.send(regID, params, 7);
				System.out.println("DANH_GIA_FEEDBACK: bn.gui=" + nguoiGui);
			}
			else if (cmd.equals(Protocol.RESULT_DANH_GIA)) {// KQ danh gia
				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));
				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
				String result = getParameter(request, Protocol.FINAL_RESULT_ESTIMATE);

				ArrayList<PairValue> params = new ArrayList<PairValue>();
				params.add(new PairValue(Protocol.CMD, cmd));
				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
				params.add(new PairValue(Protocol.FINAL_RESULT_ESTIMATE, result));
				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);

				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
				ClientSender.send(dsIDDevices, params, 5);
				System.out.println("RESULT_DANH_GIA: bn.gui=" + nguoiGui);
			}
//			else if (cmd.equals(Protocol.CLEAR_NODE_TREE)) {// dung thao luan
//
//				String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
//				int idNhom = Integer.parseInt(getParameter(request, Protocol.ID_NHOM));
//
//				ArrayList<PairValue> params = new ArrayList<PairValue>();
//				params.add(new PairValue(Protocol.CMD, cmd));
//				params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
//				ArrayList<NguoiDungPOJO> arrMember = new NguoiDungDA().getNguoiDungCuaNhom(idNhom);
//
//				ArrayList<String> dsIDDevices = Common.retrieveRegId(arrMember);
//				ClientSender.send(dsIDDevices, params, 5);
//				
//				System.out.println("CLEAR_NODE_TREE: bn.gui=" + nguoiGui) ;
//			}

		} catch (Exception e) {
			out.print(Protocol.SERVER_ERROR);
			if (Protocol.DEBUG == 1) {
				e.printStackTrace(out);
				e.printStackTrace(System.out);
			}
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected String getParameter(HttpServletRequest request, String parameter) throws ServletException {
		String value = request.getParameter(parameter);
		if (value == null || value.trim().isEmpty()) {
			throw new ServletException("Parameter " + parameter + " not found");
		}
		return value.trim();
	}

	protected String getParameter(HttpServletRequest req, String parameter, String defaultValue) {
		String value = req.getParameter(parameter);
		if (value == null || value.trim().isEmpty()) {
			value = defaultValue;
		}
		return value.trim();
	}

}
